(function() {
    angular
        .module('ydApp.register', [])
        .config(function($stateProvider) {
            $stateProvider.state('register', {
                url: '/register',
                controller: "registerCtrl",
                templateUrl: 'views/user/register.html'
            });
            $stateProvider.state('register-user', {
                url: '/register-user',
                controller: "registerUserCtrl",
                templateUrl: 'views/user/register-user.html'
            });
            $stateProvider.state('register-doc', {
                url: '/register-doc',
                controller: "registerDocCtrl",
                templateUrl: 'views/user/register-doc.html'
            });
            $stateProvider.state('register-doc-info', {
                cache: false,
                url: '/register-doc-info',
                controller: "registerDocInfoCtrl",
                templateUrl: 'views/user/register-doc-info.html'
            });
            $stateProvider.state('register-doc-info-hospital', {
                url: '/register-doc-info-hospital:state',
                controller: "registerDocInfoHospitalCtrl",
                templateUrl: 'views/user/register-doc-info-hospital.html'
            });
            $stateProvider.state('register-doc-info-depart', {
                cache: false,
                url: '/register-doc-info-depart:state',
                controller: "registerDocInfoDepartCtrl",
                templateUrl: 'views/user/register-doc-info-depart.html'
            });
            $stateProvider.state('register-doc-info-depart2', {
                url: '/register-doc-info-depart2:state',
                controller: "registerDocInfoDepart2Ctrl",
                templateUrl: 'views/user/register-doc-info-depart2.html'
            });
            $stateProvider.state('register-doc-info-position', {
                url: '/register-doc-info-position:state',
                controller: "registerDocInfoPositionCtrl",
                templateUrl: 'views/user/register-doc-info-position.html'
            });
            $stateProvider.state('register-doc-info-succ', {
                url: '/register-doc-info-succ',
                controller: 'registerDocInfoSuccCtrl',
                templateUrl: 'views/user/register-doc-info-succ.html'
            });
            $stateProvider.state('register-noduty', {
                url: '/register-noduty',
                templateUrl: 'views/user/register-noduty.html'
            });
        })
        .controller("registerCtrl", registerCtrl)
        .controller("registerUserCtrl", registerUserCtrl)
        .controller("registerDocCtrl", registerDocCtrl)
        .controller("registerDocInfoCtrl", registerDocInfoCtrl)
        .controller("registerDocInfoHospitalCtrl", registerDocInfoHospitalCtrl)
        .controller("registerDocInfoDepartCtrl", registerDocInfoDepartCtrl)
        .controller("registerDocInfoDepart2Ctrl", registerDocInfoHospitalCtrl)
        .controller("registerDocInfoSuccCtrl", registerDocInfoSuccCtrl)
        .controller("registerDocInfoPositionCtrl", registerDocInfoPositionCtrl);

    function registerCtrl($scope, $state) {
        var vm = $scope.vm = {};
        vm.go = function(router) {
            $state.go(router);
        }
    }

    function registerUserCtrl($scope, $timeout, xhr, popup, $state) {
        var vm = $scope.vm = {};
        vm.user = {
            userType: 1
        };
        vm.enable = true;
        vm.getCode = function() {
            if (!vm.enable) {
                return;
            }
            if (vm.user.code) {
                xhr.get('HealthInquiry/com/sms/send', {
                    phone: vm.user.code
                }).then(function(result) {
                    vm.time = 60;
                    $scope.timeInteval();
                    vm.enable = false;
                });
            } else {
                popup.alert("请输入手机号码");
            }

        }

        $scope.timeInteval = function() {
            vm.time--;
            if (vm.time > 0) {
                $timeout($scope.timeInteval, 1000);
            } else {
                vm.enable = true;
            }
        }

        vm.register = function() {

            if (!vm.user.code) {
                popup.alert("请输入手机号码");
                return;
            }

            if (!vm.user.valiCode) {
                popup.alert("请输入验证码");
                return;
            }

            if (!vm.user.password) {
                popup.alert("请输入验密码");
                return;
            }
            if (!angular.equals(vm.user.password, vm.user.repassword)) {
                popup.alert("两次密码输入不一致");
                return;
            }
            if (!vm.agree) {
                popup.alert("您需要同意用户条款");
                return;
            }
            xhr.get('HealthInquiry/com/user/register', vm.user).then(function(result) {
                popup.alert(result.resultMsg);
                $state.go('login');
            });
        }

    }

    function registerDocCtrl($scope, $timeout, xhr, popup, $state, localStore) {
        var vm = $scope.vm = {};
        vm.enable = true;
        vm.user = {
            userType: 2
        };

        vm.getCode = function() {
            if (!vm.enable) {
                return;
            }
            if (vm.user.code) {
                xhr.get('HealthInquiry/com/sms/send', {
                    phone: vm.user.code
                }).then(function(result) {
                    vm.time = 60;
                    $scope.timeInteval();
                    vm.enable = false;
                });
            } else {
                popup.alert("请输入手机号码");
            }

        }

        $scope.timeInteval = function() {
            vm.time--;
            if (vm.time > 0) {
                $timeout($scope.timeInteval, 1000);
            } else {
                vm.enable = true;
            }
        }


        vm.register = function() {

            if (!vm.user.code) {
                popup.alert("请输入手机号码");
                return;
            }

            if (!vm.user.valiCode) {
                popup.alert("请输入验证码");
                return;
            }

            if (!vm.user.password) {
                popup.alert("请输入验密码");
                return;
            }
            if (!angular.equals(vm.user.password, vm.user.repassword)) {
                popup.alert("两次密码输入不一致");
                return;
            }
            if (!vm.agree) {
                popup.alert("您需要同意用户条款");
                return;
            }
            xhr.get('HealthInquiry/com/user/register', vm.user).then(function(result) {
                vm.user = result.data;
                vm.user.name = "";
                vm.user.email = "";
                localStore.save('user', vm.user);


                $state.go('register-doc-info', {
                    userId: vm.user.id
                });
            });
        }


    }

    function registerDocInfoCtrl($scope, $stateParams, xhr, localStore, $state, popup) {
        var vm = $scope.vm = {};
        $scope.vm.user = localStore.get('user');
        console.log(vm.user)

        // vm.user.branchName="asdasd";

        $scope.go = function(router) {
            $state.go(router)
        }


        $scope.register = function() {
            vm.user.deptPhone = vm.user.deptPhone_head + vm.user.deptPhone_body;
            vm.user.orderNo = 1;
            vm.user.userId = vm.user.id;
            console.log(vm.user)
            xhr.get("HealthInquiry/com/doctor/register", vm.user).then(function() {
                localStore.del("user");
                // popup.alert("提交成功");
                $state.go("register-doc-info-succ");
            });
        }

        $scope.$watch('vm.user', function(newValue, oldValue, scope) {
            localStore.save("user", vm.user);
        }, true);
    }


    function registerDocInfoHospitalCtrl($scope, xhr, $state, localStore, $stateParams) {

        var vm = $scope.vm = {};
        vm.hosList = [];

        vm.condition = {
            pageIndex: 0,
            pageSize: 20,
            // sortField:'',
            // sortOrder:'asc',
            name: ''
        }
        $scope.search = function() {
            return xhr.get("HealthInquiry/com/hospital/findByCondition", vm.condition)
                .then(function(result) {
                    // vm.hosList = result.data;
                    if (result.data.length <= 0) {
                        vm.nodata = true;
                    } else {
                        vm.hosList = vm.hosList.concat(result.data);
                        vm.nodata = false;
                    }

                });
        }


        $scope.doRefresh = function() {
            vm.condition.pageIndex = 0;
            vm.hosList = [];
            $scope.search().then(function() {
                $scope.$broadcast('scroll.refreshComplete');

            });

        }

        $scope.loadMore = function() {
            console.log(vm.condition.pageIndex);
            vm.condition.pageIndex++;
            $scope.search().then(function() {
                $scope.$broadcast('scroll.infiniteScrollComplete');
            });

        }

        $scope.selectHospital = function(hos) {
            console.log(hos)
            var user = localStore.get("user");
            user.hospitalName = hos.name;
            user.hospitalId = hos.id;
            localStore.save("user", user);
            var state = $stateParams.state;
            if (state) {
                if (angular.equals($stateParams.state, "userInfo-doc")) {
                    $state.go($stateParams.state, { edit: 1 });
                } else {
                    $state.go($stateParams.state);
                }
            } else {
                $state.go('register-doc-info');
            }
        }

        $scope.search();


        $scope.$watch('vm.condition.name', function(newValue, oldValue, scope) {
            $scope.doRefresh();
        });
    }

    function registerDocInfoDepartCtrl($window,$scope, xhr, localStore, $state, $stateParams) {
        var vm = $scope.vm = {};

        vm.condition = {
            pageIndex: 0,
            pageSize: 50,
            // sortField:'',
            // sortOrder:'asc',
            name: ''
        }
        $scope.search = function() {
            xhr.get("HealthInquiry/com/branch/findByCondition", vm.condition)
                .then(function(result) {
                    vm.list = result.data;
                });
        }


        $scope.selected = function(branch) {
            //查询子科室
            if (branch.hasChildren == 1) {
                vm.condition.parentId = branch.id;
                $scope.search();
            } else {
                var user = localStore.get("user");
                user.branchName = branch.name;
                user.branchId = branch.id;
                localStore.save("user", user);
                if ($stateParams.state) {
                    if (angular.equals($stateParams.state, "userInfo-doc")) {
                        $state.go($stateParams.state, { edit: 1 });
                    } else if (angular.equals($stateParams.state, "add-check")||angular.equals($stateParams.state, "edit-check")) {
                        $window.history.back();
                    }
                    else{
                        $state.go($stateParams.state);
                    }
                } else {
                    $state.go('register-doc-info');
                }

            }
        }

        $scope.search();


        $scope.$watch('vm.condition', function(newValue, oldValue, scope) {
            $scope.search();
        }, true);
    }


    function registerDocInfoPositionCtrl($scope, xhr, localStore, $state, $stateParams) {
        var vm = $scope.vm = {};


        vm.condition = {
            pageIndex: 0,
            pageSize: 50,
            // sortField:'',
            // sortOrder:'asc',
            name: ''
        }

        $scope.search = function() {

            xhr.get("HealthInquiry/com/professionTitle/findByCondition", vm.condition)
                .then(function(result) {
                    vm.list = result.data;
                });
        }


        $scope.selected = function(item) {
            var user = localStore.get("user");
            user.titleName = item.name;
            user.titleId = item.id;
            localStore.save("user", user);
            var state = $stateParams.state;
            console.log(state)
            if (state) {
                if (angular.equals($stateParams.state, "userInfo-doc")) {
                    $state.go($stateParams.state, { edit: 1 });
                } else {
                    $state.go($stateParams.state);
                }
            } else {
                $state.go('register-doc-info');
            }

        }

        $scope.search();


        $scope.$watch('vm.condition', function(newValue, oldValue, scope) {
            $scope.search();
        }, true);
    }


    function registerDocInfoSuccCtrl($scope, $state) {
        $scope.go = function(router) {
            $state.go(router);
        }
    }
})();
